<?php 

namespace app\controller\appPage;

use app\exception\ErrnoPlus;
use app\service\HelpProblemInfoService;
use app\validate\HelpProblemInfoValidate;
use share\controller\PageBaseController;
use share\exception\PageException;

class HelpProblemInfoController extends PageBaseController{

    public function initValidate(){  
        $this->validate = new HelpProblemInfoValidate();
    }


    public function doc_action(){
        // $input = $this->check();
        $language = $this->request->param('language');

        if($language != 'CN'){
            $language = 'EN';
        }
        $lang = 'en';
        if($language == 'CN'){
            $lang = 'zh-cn';
        }
        $info = [
            "accountSoft" => lang("Account",[],$lang),
            "deviceConnection" => lang("Connection",[],$lang),
            "playBack" => lang("Replay",[],$lang),
            "deviceSet" => lang("Setting",[],$lang),
            "managent" => lang("Managent",[],$lang),
            "moreProblem" => lang("More",[],$lang),
            "hotProblem" => lang("Hotspot Problem",[],$lang),
            "more" => lang("More",[],$lang),
            "welcome" => lang("Hi, we are pleased to serve you",[],$lang)
        ];
        $hotList = HelpProblemInfoService::getHotBaseInfoList(['language'=>$language]);  
        $type = HelpProblemInfoService::getTypeListInfo();
        $info['hotList'] = $hotList;

        setcookie("currentLanguage",$language,time() + 86400,'/');
        
        return self::ok([
            'info' => $info,
            'typeList' => $type,
        ]);
    }

    public function type_action(){
        $language = $this->request->cookie('currentLanguage');

        if($language != 'CN'){
            $language = 'EN';
        }
        $lang = 'en';
        if($language == 'CN'){
            $lang = 'zh-cn';
        }
        $type = $this->request->param("type");
        if (!$type) {
            $info = [
                "title" => lang("Help",[],$lang),
                "msg" => lang("In development",[],$lang)
            ];
            return self::ok(['info'=>$info],'help4CN.html');            
        } 
        $res = HelpProblemInfoService::getAllByType(["type" => $type, "language" => $language]);
        $info = [
            "problemListName" => lang("Problem List",[],$lang),
            "typeName" => lang($type,[],$lang)
        ];
        $info["problemList"] = $res;
        $totalString = lang("A total of {total} records",[],$lang);
        $info["total"] = str_replace("{total}", count($res), $totalString);
        return self::ok(['info'=>$info]);  
    }

    public function detail_action(){
        $language = $this->request->cookie('currentLanguage');

        if($language != 'CN'){
            $language = 'EN';
        }
        $lang = 'en';
        if($language == 'CN'){
            $lang = 'zh-cn';
        }
        $id = $this->request->param("id");
        $info = [
            "problemListName" => lang("Problem List",[],$lang),
            "errorString" => ""
        ];
        if (!$id) {
            $info["errorString"] = lang("The problem has been closed by the administrator",[],$lang);
            return self::ok(['info'=>$info]);  
        }
        $detail =  HelpProblemInfoService::detail($id);
        if(empty($detail)){
            $info["errorString"] = lang("The problem has been closed by the administrator",[],$lang);
            return self::ok(['info'=>$info]);  
        }
        $info = array_merge($info,$detail);
        HelpProblemInfoService::addVisit($detail['id']);
        return self::ok(['info'=>$info]);  
    }
}